<template>
  <div class="home">
    <a-typography-title :level="1">今日推荐</a-typography-title>
    <div class="main-container">
      <div class="featured-section">
        <div class="section-header">
          <a-typography-title :level="2">精选题目</a-typography-title>
          <router-link to="/problems">查看更多</router-link>
        </div>
        <ProblemList :problems="featuredProblems" />
      </div>

      <div class="featured-section">
        <div class="section-header">
          <a-typography-title :level="2">精选文章</a-typography-title>
          <router-link to="/blogs">查看更多</router-link>
        </div>
        <div class="article-list">
          <!-- <router-link
            v-for="article in featuredArticles"
            :key="article.id"
            class="article-item"
            :to="`/blogs/${article.id}`"
          >
            <div class="article-header">
              <a-avatar :src="article.avatar" size="small" />
              <div class="article-meta">
                <a-typography-title :level="4">{{ article.title }}</a-typography-title>
                <div class="article-author">
                  <span>{{ article.author }}</span>
                  <span class="article-date">{{ article.date }}</span>
                </div>
              </div>
            </div>
            <div class="article-summary">
              {{ article.summary }}
            </div>
            <div class="card-stats">
              <span>阅读: {{ article.readCount }}</span>
            </div>
          </router-link> -->
          <BlogList :blogs="featuredArticles" @blog-click="onBlogClick" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useUserStore } from '@/stores/user'
import { useHomeStore } from '@/stores/home'
import { onMounted } from 'vue'
import ProblemList from '@/components/ProblemList.vue'
import BlogList from '@/components/BlogList.vue'

export default {
  name: 'HomeView',
  components: {
    ProblemList,
    BlogList
  },
  setup() {
    const userStore = useUserStore()
    const homeStore = useHomeStore()
    
    onMounted(async () => {
      await homeStore.fetchHomeData()
    })

    return { 
      userStore,
      homeStore
    }
  },
  computed: {
    solvedProblems() {
      return this.userStore.userStats.solvedProblems
    },
    featuredProblems() {
      return this.homeStore.featuredProblems.map(p => ({
        problemId: p.id,
        problemTitle: p.title,
        difficultyLevel: p.difficulty,
        acceptanceRate: p.acceptanceRate.toFixed(2),
        solved: p.solved
      }))
    },
    featuredArticles() {
      return this.homeStore.featuredArticles
    }
  },
  methods: {
    getDifficultyTagType(difficulty) {
      switch (difficulty) {
        case 'easy': return 'green'
        case 'medium': return 'orange'
        case 'hard': return 'red'
        default: return 'blue'
      }
    },
    isProblemSolved(problemId) {
      return this.userStore.userStats.solvedProblems && this.userStore.userStats.solvedProblems.includes(problemId)
    },
    onBlogClick(blog) {
      this.$router.push(`/blogs/${blog.id}`)
    }
  }
}
</script>

<style scoped>
.home {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.main-container {
  width: 60%;
  margin: 20px 20% 20px 20%;
}

.featured-section {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e8e8e8;
  padding: 20px;
  margin-bottom: 30px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.problem-list {
  display: flex;
  flex-direction: column;
}

.problem-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.problem-info {
  display: flex;
  align-items: center;
  width: 100%;
}

.problem-id {
  color: #666;
  min-width: 40px;
  margin-right: 15px;
}

.problem-title {
  font-weight: 500;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.problem-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-left: auto;
}

.difficulty-tag {
  min-width: 60px;
  text-align: center;
}

.acceptance-rate {
  color: #666;
  min-width: 80px;
  text-align: right;
}

.solved-icon {
  color: #52c41a;
  margin-left: 8px;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.article-item {
  padding: 15px;
  border-radius: 4px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.1);
  cursor: pointer;
}

.article-header {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
}

.article-meta {
  flex: 1;
}

.article-author {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 4px;
}

.article-date {
  color: #999;
  font-size: 0.8em;
}

.article-summary {
  color: #666;
  margin: 8px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
